package com.yy.web.pm.patrol.controller;

import com.yy.web.pm.alarm.domain.AlarmDate;
import com.yy.web.pm.alarm.mapper.AlarmDateMapper;
import com.yy.web.pm.alarm.service.AlarmDateService;
import com.yy.web.pm.billboardRemove.domain.BillboardRemove;
import com.yy.web.pm.patrol.domain.PeoplePatrol;
import com.yy.web.pm.patrol.dto.*;
import com.yy.web.pm.patrol.service.FileStorageService;
import com.yy.web.pm.patrol.service.PeoplePatrolService;
import com.yy.web.pm.until.Result;
import com.yy.web.pm.until.JwtUtils;
import com.yy.web.pm.user.domain.User;
import com.yy.web.pm.user.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yy.web.pm.alarm.service.Impl.AlarmDateServiceImpl.extractOpenId;

@RestController
@RequestMapping("/patrol")
@Api(tags = "巡检管理", description = "管理巡检和告警的端点")
public class PatrolController {
    @Qualifier("peoplePatrolServiceImpl")
    @Autowired
    private PeoplePatrolService patrolService;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private AlarmDateMapper alarmDateMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtUtils jwtUtils;
    @Value("${file.upload-dir}")
    private String fileAddress;



    //
    // //新增数据
    // @PostMapping("/add")
    // @ApiOperation(
    //         value = "新增巡检",
    //         response = Result.class
    // )
    // public Result add(@RequestBody PeoplePatrol patrol) {
    //     // 多个图片地址获取，放入到实体类进行数据插入
    //     return Result.success(patrolService.insert(patrol));
    // }
    //
    // //修改数据（巡检提交）
    // @PutMapping("/update")
    // @ApiOperation(value = "内容巡检",response = Result.class)
    // public Result update(@RequestBody PeoplePatrol patrol) {
    //     return Result.success(patrolService.updateByPrimaryKey(patrol));
    // }
    //
    // //删除数据
    // @DeleteMapping("/delete")
    // @ApiOperation(value = "删除巡检",response = Result.class)
    // public Result delete(@RequestParam("id") Long id) {
    //     return Result.success(patrolService.deleteByPrimaryKey(Math.toIntExact(id)));
    // }
    // //巡检列表
    // @GetMapping("/list")
    // @ApiOperation(value = "巡检列表",response = Result.class)
    // public Result list(PeoplePatrol patrol) {
    //     return Result.success(patrolService.selectListAll(patrol));
    // }
    // //告警列表
    // @GetMapping("/alarmList")
    // @ApiOperation(value = "告警列表",response = Result.class)
    // public Result alarmList(AlarmDate patrol) {
    //     return Result.success(alarmDateMapper.selectAlarmAll(patrol));
    // }

    //拆除登记
    // @PostMapping("/removeList")
    // @ApiOperation(value = "拆除列表",response = Result.class)
    // public Result removeList(@RequestBody BillboardRemove billboardRemove) {
    //     return Result.success(patrolService.insertRemoveAll(billboardRemove));
    // }
    //
    // //巡检展示,显示当前用户的所有巡检数据
    // @GetMapping("/show")
    // @ApiOperation(value = "巡检展示",response = Result.class)
    // public Result show(PatroDto patrol) {
    //  return Result.success(patrolService.selectShowList(patrol));
    // }
    // //紧急巡检
    // @GetMapping("/urgent")
    // @ApiOperation(value = "紧急巡检(巡检消息列表)",response = Result.class)
    // public Result urgent(PatroDto patrol) {
    //     return Result.success(patrolService.urgent(patrol));
    // }


    //-------------------------------------------分界线

//巡检列表
    @GetMapping("/patrolList")
    @ApiOperation(value = "巡检列表",response = Result.class)
    public Result patrolList(PatrolDto patrol) {
        return Result.success(patrolService.selectPatrolList(patrol));
    }
    //巡检详情
    @GetMapping("/detail")
    @ApiOperation(
            value = "通过ID获取巡检详情",
            notes = "根据ID获取特定巡检的详细信息",
            response = Result.class
    )
    public Result detail(@RequestParam("id") Long id) {
        return Result.success(patrolService.selectByPrimaryKey(Math.toIntExact(id)));
    }
    //新增巡检
    @PostMapping("/insertPatrol")
    @ApiOperation(value = "新增巡检",response = Result.class)
    public Result insertPatrol(@RequestBody PeoplePatrol patrol) {
        return Result.success(patrolService.insert(patrol));
    }
    //告警详情
    @GetMapping("/alarmDetail")
    @ApiOperation(value = "告警详情",response = Result.class)
    public Result alarmDetail(@RequestParam("id") Long id) {
        return  Result.success(alarmDateMapper.selectByPrimaryKey(Math.toIntExact(id)));
    }
    //安全警告
    @GetMapping("/safe")
    @ApiOperation(value = "安全警告",response = Result.class)
    public Result safe(PeoplePatrol patrol) {
        //获取当前用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String openId = extractOpenId(authentication.getName());
        User user = userMapper.getUserByOpenId(openId);//获取当前用户信息
        patrol.setUserId(Math.toIntExact(user.getId()));
        return Result.success(alarmDateMapper.selectAlarmList(patrol));
    }
    //拆除登记
    @PostMapping("/removeList")
    @ApiOperation(value = "拆除列表",response = Result.class)
    public Result removeList(@RequestBody BillboardRemove billboardRemove) {
        return Result.success(patrolService.insertRemoveAll(billboardRemove));
    }
    //巡检消息接口
    @GetMapping("/patrolMessage")
    @ApiOperation(value = "巡检消息接口",response = Result.class)
    public Result patrolMessage(PatroLMessage patrol) {
        return Result.success(patrolService.selectPatrolMessage(patrol));
    }
    //告警数据录入
    @PostMapping("/alarmData")
    @ApiOperation(value = "告警数据录入",response = Result.class)
    public Result alarmData(@RequestBody AlarmDate alarmDate) {
        return Result.success(alarmDateMapper.insert(alarmDate));
    }

    //查看拆除信息
    @GetMapping("/removeInfo")
    @ApiOperation(value = "查看拆除信息",response = Result.class)
    public Result removeInfo(@RequestParam("shopId")String shopId) {
        return Result.success(patrolService.selectRemoveInfo(shopId));
    }

    //     图片上传接口
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        try {
            // 1. 存储文件到本地
            String fileName = fileStorageService.storeAutoModuleFile(file);
            // 2. 构建返回结果
            Map<String, String> response = new HashMap<>();
            response.put("status", "success");
            response.put("fileName", fileName);
            response.put("fileUrl", fileAddress.replace("**", fileName));
            response.put("message", "图片上传成功");
            return Result.success(response);
        } catch (Exception e) {
            return Result.error(500, "图片上传失败: " + e.getMessage());
        }

    }
}

